package com.winsdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winsdom.entity.CustomerNotificationConfig;
import com.winsdom.mapper.CustomerNotificationConfigMapper;
import com.winsdom.service.ICustomerNotificationConfigService;
import com.winsdom.vo.CustomerNotificationConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户通知配置表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-12
 */
@Slf4j
@Service
public class CustomerNotificationConfigServiceImpl extends ServiceImpl<CustomerNotificationConfigMapper, CustomerNotificationConfig> implements ICustomerNotificationConfigService {

    @Override
    public List<CustomerNotificationConfigVO> getConfigByCustomerId(Long customerId) {
        log.info("查询客户通知配置，客户ID：{}", customerId);
        LambdaQueryWrapper<CustomerNotificationConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerNotificationConfig::getCustomerId, customerId);
        List<CustomerNotificationConfig> configs = list(wrapper);
        
        List<CustomerNotificationConfigVO> voList = new ArrayList<>();
        for (CustomerNotificationConfig config : configs) {
            CustomerNotificationConfigVO vo = new CustomerNotificationConfigVO();
            BeanUtils.copyProperties(config, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(CustomerNotificationConfigVO configVO) {
        log.info("开始更新客户通知配置，配置信息：{}", configVO);
        
        // 参数校验
        Assert.notNull(configVO, "配置信息不能为空");
        Assert.notNull(configVO.getCustomerId(), "客户ID不能为空");
        log.info("参数校验通过，客户ID：{}", configVO.getCustomerId());

        // 查询现有配置
        LambdaQueryWrapper<CustomerNotificationConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerNotificationConfig::getCustomerId, configVO.getCustomerId());
        CustomerNotificationConfig existingConfig = getOne(wrapper);
        
        // 校验配置是否存在
        Assert.notNull(existingConfig, "客户通知配置不存在");
        log.info("找到现有配置：{}", existingConfig);

        // 只更新非空字段
        if (configVO.getPaymentNotifyEnabled() != null) {
            existingConfig.setPaymentNotifyEnabled(configVO.getPaymentNotifyEnabled());
        }
        if (configVO.getPaymentNotifyType() != null) {
            existingConfig.setPaymentNotifyType(configVO.getPaymentNotifyType());
        }
        if (configVO.getCollectionNotifyEnabled() != null) {
            existingConfig.setCollectionNotifyEnabled(configVO.getCollectionNotifyEnabled());
        }
        if (configVO.getCollectionNotifyType() != null) {
            existingConfig.setCollectionNotifyType(configVO.getCollectionNotifyType());
        }
        if (configVO.getCollectionAfterDays() != null) {
            existingConfig.setCollectionAfterDays(configVO.getCollectionAfterDays());
        }
        if (configVO.getFirstNoticeInterval() != null) {
            existingConfig.setFirstNoticeInterval(configVO.getFirstNoticeInterval());
        }
        if (configVO.getMaxNoticeTimes() != null) {
            existingConfig.setMaxNoticeTimes(configVO.getMaxNoticeTimes());
        }
        if (configVO.getCurrentNoticeTimes() != null) {
            existingConfig.setCurrentNoticeTimes(configVO.getCurrentNoticeTimes());
        }

        // 更新配置
        boolean success = updateById(existingConfig);
        log.info("更新客户通知配置{}，更新后的配置：{}", success ? "成功" : "失败", existingConfig);
    }
}
